package com.txl.lzxl.webjx;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.Map;

/**
 * Created by feq on 2016/12/8.
 */

public class Adapter extends RecyclerView.Adapter<Adapter.Myholder> implements View.OnClickListener {

    Context context;
    TypeName typeName;
    MyItemClickListener myItemClickListener;

    public Adapter(Context context, TypeName typeName) {
        this.context = context;
        this.typeName = typeName;
    }

    @Override
    public Myholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_rc, null);
        return new Myholder(view);
    }

    @Override
    public void onBindViewHolder(Myholder holder, int position) {
        Map<String, String> map = typeName.getNames().get(position);
        for (String entry : map.keySet()) {
            holder.mTvName.setText(entry);
            holder.mTvName.setOnClickListener(this);
            holder.mTvName.setTag(entry);
        }

    }


    @Override
    public int getItemCount() {
        return typeName.getNames().size();
    }

    @Override
    public void onClick(View view) {
        if (null != myItemClickListener) {
            myItemClickListener.onItemClick(view, (String) view.getTag());
        }
    }

    class Myholder extends RecyclerView.ViewHolder {
        TextView mTvName;

        public Myholder(View itemView) {
            super(itemView);
            mTvName = (TextView) itemView.findViewById(R.id.tv_name);
        }

    }

    public interface MyItemClickListener {
        public void onItemClick(View view, String name);
    }

    public void setOnItemClickListener(MyItemClickListener listener) {
        this.myItemClickListener = listener;
    }
}
